package com.bkaka.controller;

import com.bkaka.bean.ResponseBean;
import com.bkaka.entity.ExamAnswerHistory;
import com.bkaka.entity.ExamQuestion;
import com.bkaka.entity.ExamQuestionOptions;
import com.bkaka.service.ExamAnswerHistoryServer;
import com.bkaka.service.ExamQuestionOptionsService;
import com.bkaka.service.ExamQuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.sql.Array;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by maxiaoqiang on 2018/9/3.
 */
@RestController
@RequestMapping(value = "${api.path}/question")
public class ExamQuestionController {

    @Autowired
    private ExamQuestionService service;

    @Autowired
    private ExamQuestionOptionsService optionsService;

    @Autowired
    private ExamAnswerHistoryServer answerHistoryServer;

    @GetMapping(value = "/list")
    public ResponseBean findAll(Integer examSubjectId, Integer examChapterId) {
        List<ExamQuestion> list = service.findAll(examSubjectId, examChapterId);
        return new ResponseBean(200, "SUCCESS", list);
    }

    @ResponseBody
    @PostMapping(value = "/create", produces = "application/json;charset=UTF-8")
    public ResponseBean create(@Valid @RequestBody ExamQuestion question) {
        //首先保存题目
        service.create(question);
        //保存选项
        List<ExamQuestionOptions> list = question.getOptionList();
        for (int i = 0; i < list.size(); i++) {
            ExamQuestionOptions newOption = new ExamQuestionOptions();
            newOption.setExamQuestionId(question.getId());
            newOption.setName(list.get(i).getName());
            optionsService.create(newOption);
        }
        return new ResponseBean(200, "SUCCESS", question);
    }

    @ResponseBody
    @PostMapping(value = "/update", produces = "application/json;charset=UTF-8")
    public ResponseBean update(@Valid @RequestBody ExamQuestion question) {
        service.update(question);
        List<ExamQuestionOptions> optionsList = question.getOptionList();
        for (int i = 0; i < optionsList.size(); i++) {
            ExamQuestionOptions option = optionsList.get(i);
            if (option.getId().toString().indexOf("-") > -1) {

                ExamQuestionOptions newOption = new ExamQuestionOptions();
                newOption.setName(optionsList.get(i).getName());
                newOption.setExamQuestionId(question.getId());
                optionsService.create(newOption);
            } else {
                optionsService.update(option);
            }
        }
        return new ResponseBean(200, "SUCCESS", null);
    }

    @ResponseBody
    @PostMapping(value = "/delete", produces = "application/json;charset=UTF-8")
    public ResponseBean delete(Integer id) {
        service.delete(id);
        return new ResponseBean(200, "SUCCESS", "");
    }

    @GetMapping(value = "/exam")
    public ResponseBean exam(Integer examPaperId) {
        System.out.println("========id========" + examPaperId);
        List<ExamAnswerHistory> list = answerHistoryServer.findByPaperId(examPaperId);
        List<Integer> arrays = new ArrayList<>();
        for (Integer i = 0; i < list.size(); i++) {
            arrays.add(list.get(i).getExamQuestionId());
        }
        List<ExamQuestion> questionList = service.examQuestions(examPaperId, arrays);
        return new ResponseBean(200, "SUCCESS", questionList);
    }

    @GetMapping(value = "/mistakes")
    public ResponseBean mistakes(Integer examSubjectId, Integer examChapterId, Integer userId) {
        List<ExamQuestion> questions = service.examMistake(examSubjectId, examChapterId, userId);
        return new ResponseBean(200, "SUCCESS", questions);
    }

}
